package com.cooldatasoft.web.cms.person.dataProvider;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;

import com.cooldatasoft.app.WOCApplication;
import com.cooldatasoft.data.Node;
import com.cooldatasoft.data.Person;
import com.cooldatasoft.data.PersonPropertyValue;
import com.cooldatasoft.web.cms.node.NodeListModel;

public class NodeListPageDataProvider extends SortableDataProvider {

	protected List<NodeListModel> resultList=new ArrayList<NodeListModel>();
	
	public NodeListPageDataProvider(){
		// set default sort
		setSort("node.id", true);
		setSort("node.nodeType",true);
		setSort("node.label",true);
		setSort("managerNameSurname",true);
		setSort("parent.label",true);
				
		listAll();
	}
	
	
	public List<NodeListModel> listAll() {
		List<Node> nodeList = WOCApplication.get().getNodeServiceProxy().getNodeList();
		
		for (Iterator<Node> iterator = nodeList.iterator(); iterator.hasNext();) {
			Node node = iterator.next();
			if(node.getId() == 1){
				continue;
			}
			NodeListModel model = new NodeListModel();
			Person manager = WOCApplication.get().getPersonServiceProxy().get(node.getManagerId());
			Node parent = WOCApplication.get().getNodeServiceProxy().get(node.getParentId());
			//personPropertyId=1 -> name
			PersonPropertyValue name = 
				WOCApplication.get().getPersonServiceProxy().getPersonPropertyValueById(manager.getId(),1);
			//personPropertyId=2 -> surname
			PersonPropertyValue surname = 
				WOCApplication.get().getPersonServiceProxy().getPersonPropertyValueById(manager.getId(),2);
			
			model.setNode(node);
			model.setManager(manager);
			model.setParent(parent);
			
			StringBuffer nameSurnameStr=new StringBuffer("");
			if(name != null && name.getValue() != null){
				nameSurnameStr.append(name.getValue());
			}
			if(surname != null && surname.getValue() != null){
				nameSurnameStr.append(" "+surname.getValue());
			}
			model.setManagerNameSurname(nameSurnameStr.toString());
			resultList.add(model);
		}
		return resultList;
	}
	
	public Iterator<NodeListModel> iterator(int first, int count) {
		List<NodeListModel> newList = new ArrayList<NodeListModel>();
        newList.addAll(resultList.subList(first, first + count));

        final String sortColumn = this.getSort().getProperty();
        final boolean ascending = this.getSort().isAscending();

        Collections.sort(newList, new Comparator() {
			public int compare(Object entity1, Object entity2) {
				PropertyModel model1 = new PropertyModel(entity1, sortColumn);
                PropertyModel model2 = new PropertyModel(entity2, sortColumn);

                Object modelObject1 = model1.getObject();
                Object modelObject2 = model2.getObject();
                if( modelObject1==null ){
                	return -1;
                }else if( modelObject2 == null ){
                	return 1;
                }else if( modelObject1==null && modelObject2 == null){
                	return 0;
                }
                int compare = ((Comparable) modelObject1).compareTo(modelObject2);

                if (!ascending)
                    compare *= -1;

                return compare;
			}
        });

        return newList.iterator();
	}

	public IModel model(final Object object) {
		return new AbstractReadOnlyModel() {

			private static final long serialVersionUID = 1L;

			public Object getObject() {
                return object;
            }
        };
	}

	public int size() {
		return resultList.size();
	}

}
